﻿//
//
// 版权所有 (c) 2021-2023 JR  
//
// 特此免费授予获得本软件的任何人以处理本软件的权利，但须遵守以下条件：在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
//
// 软件按“原样”提供，不提供任何形式的明示或暗示的保证，包括但不限于对适销性、适用性和非侵权的保证。
// 在任何情况下，作者或版权持有人均不对任何索赔、损害或其他责任负责，无论是因合同、侵权或其他方式引起的，与软件或其使用或其他交易有关。

using Nest;
using NewLife.Serialization;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EMC.VISITOR.Core;
/// <summary>
/// 审批流程模版表
/// </summary>
[SugarTable("SysAFTemplate", "审批流程模版表")]
[SysTable]
public class SysAuditFlowTemplate : EntityBase
{
    [SugarColumn(IsNullable = true, ColumnDescription = "类型名称")]
    public string Name { get; set; }
    [SugarColumn(IsNullable = true, ColumnDescription = "所属分组")]
    public string GroupName { get; set; }
    /// <summary>
    /// 状态，0：停用  1：启用
    /// </summary>
    [SugarColumn(IsNullable = true, ColumnDescription = "状态，0：停用  1：启用")]
    public bool Status { get; set; } = true;
    /// <summary>
    /// 版本从1.0开始往上增
    /// </summary>
    [SugarColumn(IsNullable = true, ColumnDescription = "版本")]
    public virtual decimal Version { get; set; } = 1.0m;

    [SugarColumn(ColumnDescription = "审批流程JSON数据", ColumnDataType = StaticConfig.CodeFirst_BigString)]
    public virtual string Content { get; set; }

    /// <summary>
    /// 初始化原生工作流模版
    /// </summary>
    /// <returns></returns>
    public static FlowTemplateInfo CreateFlowTemplate(string content)
    {
        if (content.IsNullOrEmpty())
        {
            var templatePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "Template", "FlowTemplate", "OriginFlowTemplate.json");
            content = FileHelper.ReadFile(templatePath, Encoding.UTF8);
        }
        var templateModel = content.ToJsonEntity<FlowTemplateInfo>();
        return templateModel;
    }
}

#region 审批流程模版配置

public class FlowTemplateInfo
{
    /// <summary>
    /// 此模版的唯一键，后台系统给它赋值
    /// </summary>
    public long? tableId { get; set; }
    /// <summary>
    /// 审批主管最大层级
    /// </summary>
    public int? directorMaxLevel { get; set; }

    /// <summary>
    /// 模版名称
    /// </summary>
    public virtual FlowTemplateWorkFlowDef workFlowDef { get; set; }
    ///// <summary>
    ///// 发起人的用户或角色或部门集合信息，为空表示发起人为：所有人
    ///// </summary>
    public virtual List<FlowTemplateNodeUserRoleDepartmentList> flowPermission { get; set; }
    ///// <summary>
    ///// 所有节点信息
    ///// </summary>
    public virtual NodeConfig nodeConfig { get; set; }

}

/// <summary>
/// 模版名称
/// </summary>
public class FlowTemplateWorkFlowDef
{
    /// <summary>
    /// 模版名称
    /// </summary>
    public string name { get; set; }

    /// <summary>
    /// 组名称（与数据表配置）
    /// </summary>
    public string groupName { get; set; }
}

public enum FlowTemplateNodeSendType
{
    /// <summary>
    /// 个人
    /// </summary>
    Personal = 1,
    /// <summary>
    /// 角色
    /// </summary>
    Role = 2,
    /// <summary>
    /// 部门
    /// </summary>
    Department = 3
}

/// <summary>
/// 节点类型
/// </summary>
public enum FlowTemplateNodeBusType
{
    发起人 = 0,
    审核人,
    抄送人,
    条件

}

/// <summary>
/// 节点选中用户或角色或部门相关信息
/// </summary>
public partial class FlowTemplateNodeUserRoleDepartmentList
{
    /// <summary>
    /// 发起人允许的ID,可能是允许人Id,可能是允许部门Id
    /// </summary>
    public string targetId { get; set; }
    /// <summary>
    ///0:代表所有人 1:个人 2：角色 3：部门
    /// </summary>
    public FlowTemplateNodeSendType? type { get; set; }
    /// <summary>
    /// 名字
    /// </summary>
    [StringLength(255)]
    public string name { get; set; }

}

/// <summary>
/// 所有节点信息
/// </summary>
public class NodeConfig
{
    /// <summary>
    /// 节点名称
    /// </summary>
    public string nodeName { get; set; }
    /// <summary>
    /// 0：发起人  1：审核人  2：抄送人  3：条件 4路由
    /// </summary>
    public FlowTemplateNodeBusType type { get; set; }
    /// <summary>
    /// 条件优先级
    /// </summary>
    public string priorityLevel { get; set; }
    /// <summary>
    /// 审批人设置 1指定成员 2主管 4发起人自选 5发起人自己 7连续多级主管
    /// </summary>
    public string settype { get; set; }
    /// <summary>
    /// 审批人数 1选一个人 2选多个人
    /// </summary>
    public string selectMode { get; set; }
    /// <summary>
    /// 选择范围 1.全公司 2指定成员 2指定角色
    /// </summary>
    public string selectRange { get; set; }
    /// <summary>
    /// 检查角色id
    /// </summary>
    public string examineRoleId { get; set; }
    /// <summary>
    /// 审批终点  最高层主管数
    /// </summary>
    public string directorLevel { get; set; }

    public string replaceByUp { get; set; }
    /// <summary>
    /// 多人审批时采用的审批方式 1依次审批 2会签
    /// </summary>
    public string examineMode { get; set; }
    /// <summary>
    /// 审批人为空时 1自动审批通过/不允许发起 2转交给审核管理员
    /// </summary>
    public string noHanderAction { get; set; }
    public string examineEndType { get; set; }
    public string examineEndRoleId { get; set; }
    /// <summary>
    /// 审批终点 第n层主管
    /// </summary>
    public string examineEndDirectorLevel { get; set; }
    /// <summary>
    /// 允许发起人自选抄送人
    /// </summary>
    public string ccSelfSelectFlag { get; set; }
    /// <summary>
    /// //当前审批是否通过校验
    /// </summary>
    public bool error { get; set; } = false;
    ///// <summary>
    ///// 当审批单同时满足以下条件时进入此流程
    ///// </summary>
    public List<FlowTemplateNodeConditionList> conditionList { get; set; }
    /// <summary>
    /// 选中用户或角色或部门相关信息
    /// </summary>
    public List<FlowTemplateNodeUserRoleDepartmentList> nodeUserList { get; set; }
    /// <summary>
    /// 子节点信息
    /// </summary>
    public virtual NodeConfig childNode { get; set; }
    /// <summary>
    /// 条件子节点
    /// </summary>
    public List<NodeConfig> conditionNodes { get; set; }
}

public partial class FlowTemplateNodeConditionList
{
    public int columnId { get; set; }
    /// <summary>
    ///1 发起人 2其他
    /// </summary>
    public int type { get; set; }
    public string conditionEn { get; set; }
    public string conditionCn { get; set; }
    /// <summary>
    /// ["", "<", ">", "≤", "=", "≥"][optType]
    /// </summary>
    public string optType { get; set; }
    /// <summary>
    /// 左侧自定义内容
    /// </summary>
    public string zdy1 { get; set; }
    /// <summary>
    /// 右侧自定义内容
    /// </summary>
    public string zdy2 { get; set; }
    /// <summary>
    /// 左侧符号 < ≤
    /// </summary>
    public string opt1 { get; set; }
    /// <summary>
    /// 侧符号 < ≤
    /// </summary>
    public string opt2 { get; set; }
    /// <summary>
    /// 条件字段名称
    /// </summary>
    public string columnDbname { get; set; }
    /// <summary>
    /// 条件字段类型
    /// </summary>
    public string columnType { get; set; }
    /// <summary>
    /// checkBox多选 其他
    /// </summary>
    public string showType { get; set; }
    /// <summary>
    /// 展示名
    /// </summary>
    public string showName { get; set; }
    /// <summary>
    /// 多选数组
    /// </summary>
    public string fixedDownBoxValue { get; set; }
}
#endregion


/// <summary>
/// 基础数据的共有父类
/// </summary>
public class PageBase
{
    public PageBase()
    {
        navigatepageNums = new List<string>();
    }
    public virtual List<string> navigatepageNums { get; set; }
    public int startRow { get; set; } = 0;
    public int endRow { get; set; } = 0;
    public int prePage { get; set; } = 0;
    public bool hasNextPage { get; set; } = false;
    public int nextPage { get; set; } = 0;
    public int pageNum { get; set; } = 1;
    public int pageSize { get; set; }
    public int navigatePages { get; set; }
    public int navigateFirstPage { get; set; }
    public int total { get; set; }
    public int pages { get; set; }
    public int size { get; set; }
    public bool isFirstPage { get; set; }
    public bool isLastPage { get; set; }
    public bool hasPreviousPage { get; set; }
    public int navigateLastPage { get; set; }
}

#region 审批流程角色-基础数据
public class FlowRoles : PageBase
{
    public virtual List<FlowRole> list { get; set; }
}
public class FlowRole
{
    public long roleId { get; set; }
    public string code { get; set; }
    public string roleName { get; set; }
    public string description { get; set; }
    public int status { get; set; } = 1;
    /// <summary>
    /// 范围
    /// </summary>
    public int scope { get; set; } = 1;
}

//{
//       "navigatepageNums": [],
//       "startRow": "1",
//       "hasNextPage": "false",
//       "prePage": "0",
//       "nextPage": "0",
//       "endRow": "8",
//       "pageSize": "0",
//       "list": [{
//           "code": "SHGLY",
//           "roleId": "1",
//           "scope": "1",
//           "roleName": "审核管理员",
//           "description": "初始化内置审批角色",
//           "status": "1"
//       }, {
//   "code": "",
//           "roleId": "2",
//           "scope": "1",
//           "roleName": "招商事业部",
//           "description": "",
//           "status": "1"
//       }, {
//   "code": "",
//           "roleId": "3",
//           "scope": "1",
//           "roleName": "互联网部门",
//           "description": "",
//           "status": "1"
//       }, {
//   "code": "",
//           "roleId": "4",
//           "scope": "1",
//           "roleName": "销售部",
//           "description": "",
//           "status": "1"
//       }, {
//   "code": "",
//           "roleId": "5",
//           "scope": "1",
//           "roleName": "战区一",
//           "description": "",
//           "status": "1"
//       }, {
//   "code": "",
//           "roleId": "6",
//           "scope": "1",
//           "roleName": "战区二",
//           "description": "",
//           "status": "1"
//       }, {
//   "code": "",
//           "roleId": "7",
//           "scope": "1",
//           "roleName": "JAVA开发",
//           "description": "",
//           "status": "1"
//       }, {
//   "code": "",
//           "roleId": "8",
//           "scope": "1",
//           "roleName": "测试审批角色",
//           "description": "",
//           "status": "1"
//       }],
//       "pageNum": "1",
//       "navigatePages": "8",
//       "navigateFirstPage": "0",
//       "total": "8",
//       "pages": "0",
//       "size": "8",
//       "isLastPage": "true",
//       "hasPreviousPage": "false",
//       "navigateLastPage": "0",
//       "isFirstPage": "true"
//   }

#endregion

#region 审批流程部门-基础数据
public class FlowDepartments
{
    public FlowDepartments()
    {
        childDepartments = new List<FlowDepartmentDetail>();
        titleDepartments = new List<FlowTitleDepartmentDetail>();
        employees = new List<DepartmentEmployee>();
    }
    public virtual List<FlowDepartmentDetail> childDepartments { get; set; }
    public virtual List<FlowTitleDepartmentDetail> titleDepartments { get; set; }

    public virtual List<DepartmentEmployee> employees { get; set; }
    public class DepartmentEmployee
    {
        public long id { get; set; }
        public string employeeName { get; set; }
        public int isLeave { get; set; } = 0;
        public bool open { get; set; } = false;
    }

    public class FlowDepartmentDetail
    {
        public long id { get; set; }
        public long parentId { get; set; }
        public string departmentKey { get; set; }
        public string departmentName { get; set; }
        public string departmentNames { get; set; }
    }

    public class FlowTitleDepartmentDetail : FlowDepartmentDetail
    {
        public long departmentId { get; set; }
    }
    //    "childDepartments": [{
    //        "departmentKey": "RLXZB_V2",
    //        "": "人力行政部",
    //        "id": "150",
    //        "parentId": "0",
    //        "departmentNames": "人力行政部"
    //    }, {
    //"departmentKey": "ZNBN",
    //        "departmentName": "法务部",
    //        "id": "324",
    //        "parentId": "0",
    //        "departmentNames": "法务部"
    //    }],
    //    "employees": [{
    //        "id": "53128111",
    //        "employeeName": "亚nan",
    //        "isLeave": "0",
    //        "open": "false"
    //    }],
    //    "titleDepartments": []
}
#endregion

#region 审批流程人员-基础数据
public class FlowEmployees: PageBase
{
    public FlowEmployees()
    {
        list = new List<FlowEmployeeDetail>();
    }
    public virtual List<FlowEmployeeDetail> list { get; set; }
    public class FlowEmployeeDetail
    {
        public long id { get; set; }
        public long employeeDepartmentId { get; set; }
        public string departmentName { get; set; }
        public string employeeName { get; set; }
        public string departmentNames { get; set; }
    }
}
#endregion

#region 审批流程条件-基础数据
public class FlowConditions
{
    /// <summary>
    /// 条件id  columnId == 0 为发起人
    /// </summary>
    public long columnId { get; set; }
    /// <summary>
    /// showType == "checkBox"为多选   [input]
    /// </summary>
    public string showType { get; set; }
    /// <summary>
    /// 显示的文字
    /// </summary>
    public string showName { get; set; }
    /// <summary>
    /// 条件自定义字段
    /// </summary>
    public string columnName { get; set; }
    /// <summary>
    /// columnType == "Double"为区间
    /// </summary>
    public string columnType { get; set; }
    /// <summary>
    /// fixedDownBoxValue 匹配 columnType == "String" && showType == "checkBox"时子选项内容
    /// </summary>
    public string fixedDownBoxValue { get; set; }
    //    //条件id  columnId == 0 为发起人
    //"columnId": "1090",
    ////columnType == "String" && showType == "checkBox"为多选  columnType == "Double"为区间
    //"showType": "input",
    //"showName": "预计采购总费用",
    ////columnName 条件自定义字段
    //"columnName": "planProcurementTotalMoney",
    //"columnType": "Double",
    ////fixedDownBoxValue 匹配 columnType == "String" && showType == "checkBox"时子选项内容
    //"fixedDownBoxValue": ""
}
#endregion